package com.jie.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.pojo.Type;
import com.jie.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/12/13:12
 * @Description:
 */
@Controller
@Slf4j
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String types(@RequestParam(defaultValue = "1",value = "page") int page,
                        @RequestParam(defaultValue = "8",value = "pageSize") Integer pageSize,
                        Model model){
        //构造分页
        Page<Type> pageInfo =new Page<>(page,pageSize);
        Page<Type> pages = typeService.page(pageInfo);
        model.addAttribute("page",pages);
        return "admin/types";
    }
    @GetMapping("/types/input")
    public String input(Model model){
        model.addAttribute("type",new Type());
        return "admin/type-input";
    }
    @PostMapping("/types")
    public String addType(@Valid Type type, BindingResult result, RedirectAttributes redirectAttributes){
        LambdaQueryWrapper<Type> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Type::getName,type.getName());
        Type one = typeService.getOne(queryWrapper);
        if(one!=null){
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        if(result.hasErrors()){
            return "admin/type-input";
        }
        boolean save = typeService.save(type);
        if(save){
            redirectAttributes.addFlashAttribute("message","新增成功");
        }else {
            redirectAttributes.addFlashAttribute("message","新增失败");
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id,Model model){
        model.addAttribute("type",typeService.getById(id));
        return  "admin/type-input";
    }

    @PostMapping("/types/{id}")
    public String editType(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes redirectAttributes){
        LambdaQueryWrapper<Type> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Type::getName,type.getName());
        Type one = typeService.getOne(queryWrapper);
        if(one!=null){
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        if(result.hasErrors()){
            return "admin/type-input";
        }
        type.setId(id);
        boolean save = typeService.updateById(type);
        if(save){
            redirectAttributes.addFlashAttribute("message","更新成功");
        }else {
            redirectAttributes.addFlashAttribute("message","更新失败");
        }
        return "redirect:/admin/types";
    }
    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
        typeService.removeById(id);
        redirectAttributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/types";
    }
}
